package cn.shengongying.api.controller;

import cn.shengongying.anon.Ignore;
import cn.shengongying.enums.PayEnum;
import cn.shengongying.service.PayFactory;
import com.alibaba.fastjson.JSON;
import com.github.wxpay.sdk.WXPayUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * author: RXK
 * date: 2022/1/11 星期二
 * desc:
 */
@Ignore
@ApiIgnore
@RestController
@RequestMapping("/boss/pay/callback")
public class CallbackController extends BaseController {

    private final PayFactory payFactory;

    public CallbackController(PayFactory payFactory) {
        this.payFactory = payFactory;
    }


    @RequestMapping("/wechat")
    public String callback(HttpServletRequest request) {
        try {
            String body = getBody(request);
            if (StringUtils.isEmpty(body)) {
                return responseFail();
            }
            Map<String, String> data = WXPayUtil.xmlToMap(body);
            return doHandleBody(data);
        } catch (Exception e) {
            logger.error("去读请求内容出现异常", e);
            return responseFail();
        }
    }



    @RequestMapping("/wechat/refund")
    public String refundCallback(HttpServletRequest request) {
        try {
            String body = getBody(request);
            if (StringUtils.isEmpty(body)) {
                return responseFail();
            }
            Map<String, String> data = WXPayUtil.xmlToMap(body);
            return handleRefundCallback(data);
        } catch (Exception e) {
            logger.error("去读请求内容出现异常", e);
            return responseFail();
        }
    }

    private String handleRefundCallback(Map<String, String> data) {
        try {
            if (isRequestSuccess(data)) {
                payFactory.handleRefund(PayEnum.wechat,data);
            }
        } catch (Exception e) {
            logger.error("处理退款回调出现异常", e);
            return responseFail();
        }
        return responseSuccess();
    }

    private String doHandleBody(Map<String, String> data) {
        Integer type = getAttach(data);
        switch (type) {
            case 1:
                return handleCallBack(data);
            default:
                return responseSuccess();
        }
    }

    private String handleCallBack(Map<String, String> data) {
        try {
            payFactory.callback(PayEnum.wechat, data);
            return responseSuccess();
        } catch (Exception e) {
            logger.error("执行回调出现异常:", e);
            return responseFail();
        }
    }

    private Integer getAttach(Map<String, String> data) {
        String attach = data.get("attach");
        return JSON.parseObject(attach)
                .getInteger("type");
    }


    private static String responseFail() {
        return "<xml>" + "<return_code>" + "<![CDATA[FAIL]]>" + "</return_code>" + "<return_msg>" + "<![CDATA[OK]]>" + "</return_msg>" + "</xml>";
    }

    private static String responseSuccess() {
        return "<xml>" + "<return_code>" + "<![CDATA[SUCCESS]]>" + "</return_code>" + "<return_msg>" + "<![CDATA[OK]]>" + "</return_msg>" + "</xml>";
    }

}
